package com.design_patterns.command;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-09-19 17:25
 **/

import java.util.ArrayList;
import java.util.List;

/**
 * 命令
 */
public class Main {

    public static void main(String[] args) {
        Content content = new Content();
//        insertAndUndo(content);
//        insertAndDelete(content);
//        doList(content);
        play(content);
    }


    public static void play(Content content) {
        List<Command> commandList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            commandList.add(new InsertCommand(content, String.valueOf(i)));
            commandList.add(new DeleteCommand(content));
        }

        commandList.forEach(command -> {
            command.doIt();
            printOut(content);
        });
    }

    public static void doList(Content content) {
        List<Command> commandList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            commandList.add(new InsertCommand(content, String.valueOf(i)));
        }
        commandList.forEach(Command::doIt);
        printOut(content);
        commandList.clear();
        for (int i = 0; i < 10; i++) {
            commandList.add(new DeleteCommand(content));
        }
        commandList.forEach(Command::doIt);
        printOut(content);
    }


    public static void insertAndDelete(Content content) {
        InsertCommand insertCommand = new InsertCommand(content, "123456");
        insertCommand.doIt();
        printOut(content);
        DeleteCommand deleteCommand = new DeleteCommand(content);
        deleteCommand.doIt();
        printOut(content);
        deleteCommand.undo();
    }

    public static void insertAndUndo(Content content) {
        InsertCommand insertCommand = new InsertCommand(content, "123456");
        insertCommand.doIt();
        System.err.println(content.toString());
        insertCommand.undo();
        System.err.println(content.toString());
    }

    public static void printOut(Content content) {
        System.err.println(content.toString());
    }
}
